/***************************************************************************//**
 * @file MSc_Quaternion.h
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2013 Marek M. Cel
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 ******************************************************************************/
#ifndef MSC_QUATERNION_H
#define MSC_QUATERNION_H

////////////////////////////////////////////////////////////////////////////////

#include <string.h>

#include "MSc_Angles.h"

////////////////////////////////////////////////////////////////////////////////

/** Quaternion class. */
class MSc_Quaternion
{
public:

    /** Constructor. */
    MSc_Quaternion();

    /** Copy constructor. */
    MSc_Quaternion( const MSc_Quaternion &qtrn );

    /** Copy constructor. */
    MSc_Quaternion( const MSc_Quaternion *qtrn );

    /** Returns quaternion constructed from given values. */
    static MSc_Quaternion getFrom( double w, double x, double y, double z );

    /** Returns quaternion constructed from given angles. */
    static MSc_Quaternion getFrom( double phi, double tht, double psi );

    /** Returns quaternion constructed from given angles. */
    static MSc_Quaternion getFrom( const MSc_Angles &angl );

    inline double  w() const { return m_w; }
    inline double  x() const { return m_x; }
    inline double  y() const { return m_y; }
    inline double  z() const { return m_z; }
    inline double& w()       { return m_w; }
    inline double& x()       { return m_x; }
    inline double& y()       { return m_y; }
    inline double& z()       { return m_z; }

    bool isValid() const;

    /** Calculates quasi-Euler Bryan's angles. */
    MSc_Angles getAngles() const;

    /** Calculates quasi-Euler Bryan's angles. */
    void getAngles( MSc_Angles &angl ) const;

    /** Calculates quasi-Euler Bryan's angles. */
    void getAngles( MSc_Angles *angl ) const;

    /**
     * Calculates quasi-Euler Bryan's angles.
     * @param phi roll angle [rad]
     * @param tht pitch angle [rad]
     * @param psi yaw angle [rad] */
    void getAngles( double &phi, double &tht, double &psi ) const;

    /**
     * Calculates quasi-Euler Bryan's angles.
     * @param phi roll angle [rad]
     * @param tht pitch angle [rad]
     * @param psi yaw angle [rad] */
    void getAngles( double *phi, double *tht, double *psi ) const;

    /**
     * Calculates quaternion value due to given
     * quasi-Euler Bryan's angles.
     * @param phi roll angle [rad]
     * @param tht pitch angle [rad]
     * @param psi yaw angle [rad] */
    void setAngles( double phi, double tht, double psi );

    /**
     * Calculates quaternion value due to given
     * quasi-Euler Bryan's angles. */
    void setAngles( const MSc_Angles &angl );

    /** Sets quaternion values. */
    void set( double w, double x, double y, double z );

    /** Assignment operator. */
    MSc_Quaternion& operator= ( const MSc_Quaternion &qtrn );

    /** Addition operator. */
    MSc_Quaternion operator+ ( const MSc_Quaternion &qtrn ) const;

    /** Negation operator. */
    MSc_Quaternion operator- () const;

    /** Subtraction operator. */
    MSc_Quaternion operator- ( const MSc_Quaternion &qtrn ) const;

    /** Multiplication operator (by scalar). */
    MSc_Quaternion operator* ( double val ) const;

    /** Division operator (by scalar). */
    MSc_Quaternion operator/ ( double val ) const;

    /** Cross product operator. */
    MSc_Quaternion operator% ( const MSc_Quaternion &qtrn ) const;

    /** Unary addition operator. */
    MSc_Quaternion& operator+= ( const MSc_Quaternion &qtrn );

    /** Unary subtraction operator. */
    MSc_Quaternion& operator-= ( const MSc_Quaternion &qtrn );

    /** Unary multiplication operator (by scalar). */
    MSc_Quaternion& operator*= ( double val );

    /** Unary division operator (by scalar). */
    MSc_Quaternion& operator/= ( double val );

    /** Equality operator. */
    bool operator== ( const MSc_Quaternion &qtrn ) const;

    /** Inequality operator. */
    bool operator!= ( const MSc_Quaternion &qtrn ) const;

private:

    double m_w;     ///<
    double m_x;     ///<
    double m_y;     ///<
    double m_z;     ///<
};

////////////////////////////////////////////////////////////////////////////////

/** Multiplication operator (by scalar). */
inline MSc_Quaternion operator* ( double val, const MSc_Quaternion &qtrn )
{
    return ( qtrn * val );
}

////////////////////////////////////////////////////////////////////////////////

#endif // MSC_QUATERNION_H
